package com.yuanluludev.lib_base.application;

import android.os.Build;
import android.os.StrictMode;
import android.webkit.WebView;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;
import com.tencent.mmkv.MMKV;
import com.yuanluludev.lib_utils.util.ProcessUtils;

/**
 * yuanLulu
 * application 基类
 */
public class BaseApplication extends MultiDexApplication {

    private static BaseApplication sContext;

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        initWebView();
    }

    private void init() {
        //application
        sContext = this;
        //init mmkv
        MMKV.initialize(this);
        //multidex
        MultiDex.install(sContext);
        //解决FileUriExposedException  不知是否会产生副作用
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
        }
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        sContext = this;
    }

    public static BaseApplication getThisApplication() {
        return sContext;
    }

    /**
     * 适配Android 9的webView不同进程
     */
    private void initWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = ProcessUtils.getCurrentProcessName();
            if (!getApplicationInfo().packageName.equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

}
